package com.shen.nettysocketchat.utils

import android.telephony.PhoneNumberUtils
import java.lang.Exception
import java.util.regex.Pattern

object StringUtil {
    private val phonePattern = Pattern
        .compile("^1\\d{10}$")

    /**
     * 去除特殊字符或将所有中文标号替换为英文标号
     *
     * @param str
     * @return
     */
    fun stringFilter(str: String): String {
        var str = str
        str =
            str.replace("【".toRegex(), "[").replace("】".toRegex(), "]").replace("！".toRegex(), "!")
                .replace("：".toRegex(), ":") // 替换中文标号
        val regEx = "[『』]" // 清除掉特殊字符
        val p = Pattern.compile(regEx)
        val m = p.matcher(str)
        return m.replaceAll("").trim { it <= ' ' }
    }

//    /**
//     * 半角转换为全角
//     *
//     * @param input
//     * @return
//     */
//    fun toDBC(input: String): String {
//        val c = input.toCharArray()
//        for (i in c.indices) {
//            if (c[i] == 12288) {
//                c[i] = 32.toChar()
//                continue
//            }
//            if (c[i] > 65280 && c[i] < 65375) c[i] = (c[i] - 65248)
//        }
//        return String(c)
//    }

    /**
     * string to int
     *
     * @param input
     * @return
     */
    fun stringtoint(input: String): Int {
        return try {
            input.toInt()
        } catch (e: Exception) {
            -1
        }
    }

    /**
     * string to long
     *
     * @param input
     * @return
     */
    fun stringtolong(input: String): Long {
        return try {
            input.toLong()
        } catch (e: Exception) {
            0L
        }
    }

    /**
     * 判断内容是否为空
     *
     * @date 2013-10-24下午4:20:03
     * @author hx
     * @param o
     * @return
     */
    fun isEmpty(o: Any?): Boolean {
        return if (null == o || o.toString().trim { it <= ' ' } == "") true else false
    }

    /**
     * 字符长度
     *
     * @date 2013-10-24下午4:20:03
     * @author hx
     * @param o
     * @return
     */
    fun getLength(o: Any?): Int {
        return if (null == o || o.toString().trim { it <= ' ' } == "") {
            0
        } else {
            o.toString().trim { it <= ' ' }.length
        }
    }

    /**
     * object to int
     *
     * @date 2014-1-3下午2:14:39
     * @author hx
     * @param o
     * @return
     */
    fun ObjectToInt(o: Any?): Int {
        return if (null == o || o.toString().trim { it <= ' ' } == "") {
            -1
        } else {
            o.toString().toInt()
        }
    }

    /**
     * 去除非法字符(换行、回车...)
     * @author liu_haifang
     * @date 2014-11-7 下午2:36:48
     * @param str
     * @return
     */
    fun rmUnqualified(str: String?): String? {
        if (!isEmpty(str)) {
            val p = Pattern.compile("\\s*|\t|\r|\n")
            val m = p.matcher(str)
            return m.replaceAll("")
        }
        return null
    }

    fun isNotEmpty(o: Any?): Boolean {
        return !(null == o || o.toString().trim { it <= ' ' } == "")
    }

    /**
     * 比较两个字符串（大小写敏感）。
     *
     * <pre>
     *
     * StringUtil.equals(null, null)   = true
     * StringUtil.equals(null, &quot;abc&quot;)  = false
     * StringUtil.equals(&quot;abc&quot;, null)  = false
     * StringUtil.equals(&quot;abc&quot;, &quot;abc&quot;) = true
     * StringUtil.equals(&quot;abc&quot;, &quot;ABC&quot;) = false
     *
    </pre> *
     *
     * @param str1
     * 要比较的字符串1
     * @param str2
     * 要比较的字符串2
     *
     * @return 如果两个字符串相同，或者都是 `null` ，则返回 `true`
     */
    fun equals(str1: String?, str2: String?): Boolean {
        return if (str1 == null) {
            str2 == null
        } else str1 == str2
    }

    /**
     * 利用正则表达式判断字符串是否是数字
     * @param str
     * @return
     */
    fun isNumeric(str: String?): Boolean {
        val pattern = Pattern.compile("[0-9]*")
        val isNum = pattern.matcher(str)
        return if (!isNum.matches()) {
            false
        } else true
    }

    /**
     * 获取较纯净的手机号码<br></br>
     * 删除前缀、空格等
     *
     * @param phone
     * @return
     */
    fun getValidPhoneNumber(phone: String?): String {
        var phone = phone
        if (phone == null) return ""
        if (phone.startsWith("0086")) {
            phone = phone.substring(4)
        }
        if (phone.startsWith("+86")) {
            phone = phone.substring(3)
        }
        PhoneNumberUtils.stripSeparators(phone)
        phone = phone.replace("-", "").replace(" ", "").trim { it <= ' ' }
        return phone
    }

    /**
     * 判断是不是一个合法的手机号码
     */
    fun isPhone(phoneNum: CharSequence?): Boolean {
        return if (isEmpty(phoneNum)) false else phonePattern.matcher(
            phoneNum
        ).matches()
    }
}